﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TaDMS.Models;

namespace TaDMS.Controllers
{
    [Authorize]
    public class ApiTaskEmployeeController : ApiController
    {
        private readonly UnitOfWork _unitOfWork = new UnitOfWork();

        [System.Web.Http.HttpGet]
        public HttpResponseMessage GetAll(int start, int limit)
        {
            int? total;
            var taskEmployees = _unitOfWork.TaskEmployeeRepository.GetData(out total, orderBy: order => order.OrderBy(v => v.EmployeeId), isPaging: true, start: start, limit: limit, includeProperties: "Employee");

            if (String.IsNullOrEmpty(_unitOfWork.EmployeeRepository.ErrorMessage))
            {
                var enumerable = taskEmployees as IList<TaskEmployee> ?? taskEmployees.ToList();
                if (enumerable.Any() && total > 0)
                {
                    var result = new
                    {
                        total = total,
                        success = true,
                        message = "success",
                        data = enumerable
                    };
                    return Request.CreateResponse(HttpStatusCode.OK, result);
                }

                var empty = new
                {
                    success = false,
                    message = "Employees is empty"
                };
                return Request.CreateResponse(HttpStatusCode.OK, empty);
            }
            else
            {
                var error = new
                {
                    success = false,
                    message = _unitOfWork.TaskEmployeeRepository.ErrorMessage
                };
                return Request.CreateResponse(HttpStatusCode.OK, error);
            }
        }

        [System.Web.Http.HttpGet]
        public HttpResponseMessage GetByTaskId(int start, int limit, int taskId)
        {
            int? total;
            var taskEmployees = _unitOfWork.TaskEmployeeRepository.GetData(out total, orderBy: o => o.OrderBy(v => v.EmployeeId), isPaging: true, start: start, limit: limit, includeProperties: "Employee", filter: task => task.TaskId == taskId);

            if (String.IsNullOrEmpty(_unitOfWork.EmployeeRepository.ErrorMessage))
            {
                var enumerable = taskEmployees as IList<TaskEmployee> ?? taskEmployees.ToList();
                if (enumerable.Any() && total > 0)
                {
                    var result = new
                    {
                        total = total,
                        success = true,
                        message = "success",
                        data = enumerable
                    };
                    return Request.CreateResponse(HttpStatusCode.OK, result);
                }

                var empty = new
                {
                    success = false,
                    message = "Employees is empty"
                };
                return Request.CreateResponse(HttpStatusCode.OK, empty);
            }
            else
            {
                var error = new
                {
                    success = false,
                    message = _unitOfWork.TaskEmployeeRepository.ErrorMessage
                };
                return Request.CreateResponse(HttpStatusCode.OK, error);
            }
        }

        [System.Web.Http.HttpGet]
        public HttpResponseMessage ResourceOfTask(int taskId)
        {
            int? total;
            var taskEmployees = _unitOfWork.TaskEmployeeRepository.GetData(total: out total, orderBy: o => o.OrderBy(v => v.EmployeeId), filter: task => task.TaskId == taskId, includeProperties: "Employee");

            if (String.IsNullOrEmpty(_unitOfWork.TaskRepository.ErrorMessage))
            {
                //var enumerable = taskEmployees as IList<TaskEmployee> ?? taskEmployees.ToList();
                if (taskEmployees.Count() > 0)
                {
                    var data = taskEmployees.Where(t => t.Employee != null).Select(v => v.Employee).ToList();

                    var result = new
                    {
                        total = total,
                        success = true,
                        message = "success",
                        data = data
                    };
                    return Request.CreateResponse(HttpStatusCode.OK, result);
                }

                var empty = new
                {
                    success = true,
                    message = "empty"
                };
                return Request.CreateResponse(HttpStatusCode.OK, empty);
            }
            else
            {
                var error = new
                {
                    success = false,
                    message = _unitOfWork.TaskRepository.ErrorMessage
                };
                return Request.CreateResponse(HttpStatusCode.OK, error);
            }
        }

        [System.Web.Http.HttpPost]
        public HttpResponseMessage Create(TaskEmployee entity)
        {
            var result = _unitOfWork.TaskEmployeeRepository.Create(entity);

            if (result != null && String.IsNullOrEmpty(_unitOfWork.Department.ErrorMessage))
            {
                int? total;
                var data = _unitOfWork.EmployeeRepository.GetData(out total, orderBy: o => o.OrderBy(v => v.EmployeeId), isPaging: false,
                                                                  filter: e => e.EmployeeId == result.EmployeeId);
                var success = new
                {
                    data = data,
                    success = true,
                    message = "Created"
                };
                return Request.CreateResponse(HttpStatusCode.OK, success);
            }
            else
            {
                var error = new
                {
                    success = false,
                    message = "Craete false: " + _unitOfWork.TaskEmployeeRepository.ErrorMessage
                };
                return Request.CreateResponse(HttpStatusCode.OK, error);
            }
        }

        [System.Web.Http.HttpPost]
        public HttpResponseMessage CreateList(List<TaskEmployee> entity)
        {
            var result = _unitOfWork.TaskEmployeeRepository.Create(entity);

            if (result != null && String.IsNullOrEmpty(_unitOfWork.Department.ErrorMessage))
            {
               var success = new
                {
                    data = result,
                    success = true,
                    message = "Created"
                };
                return Request.CreateResponse(HttpStatusCode.OK, success);
            }
            else
            {
                var error = new
                {
                    success = false,
                    message = "Craete false: " + _unitOfWork.TaskEmployeeRepository.ErrorMessage
                };
                return Request.CreateResponse(HttpStatusCode.OK, error);
            }
        }

        [System.Web.Http.HttpPost]
        public HttpResponseMessage UpdateList(List<Employee> source, int taskId)
        {
            var result = _unitOfWork.TaskEmployee.UpdateList(source, taskId);

            if (result)
            {
                var success = new
                {
                    data = "Success",
                    success = true,
                    message = "Created"
                };
                return Request.CreateResponse(HttpStatusCode.OK, success);
            }
            else
            {
                var error = new
                {
                    success = false,
                    message = "Craete false: " + _unitOfWork.TaskEmployee.ErrorMessage
                };
                return Request.CreateResponse(HttpStatusCode.OK, error);
            }
        }

        [System.Web.Http.HttpPost]
        public HttpResponseMessage Delete(TaskEmployee entity)
        {
            var result = _unitOfWork.TaskEmployeeRepository.Delete(entity);

            if (result != null && String.IsNullOrEmpty(_unitOfWork.Department.ErrorMessage))
            {
                var success = new
                {
                    data = result,
                    success = true,
                    message = "Deleted"
                };
                return Request.CreateResponse(HttpStatusCode.OK, success);
            }
            else
            {
                var error = new
                {
                    success = false,
                    message = "Delete false: " + _unitOfWork.TaskEmployeeRepository.ErrorMessage
                };
                return Request.CreateResponse(HttpStatusCode.OK, error);
            }
        }
    }
}
